<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <!-- 搜索区域 -->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">菜单名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-normal"  lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <!-- 工具栏 -->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 新增 </button>
            </div>
        </script>

        <!-- 表格 -->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <!-- 表格操作列模板 -->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        <!-- 添加表单 -->
        <div id="add" style="display: none">
            <div class="layui-form layuimini-form" lay-filter="addForm">
                <div class="layui-form-item">
                    <label class="layui-form-label required">菜单名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" lay-verify="required" lay-reqtext="菜单名称不能为空"
                               placeholder="请输入菜单名称" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">菜单类型</label>
                    <div class="layui-input-inline">
                        <select name="menuType" lay-verify="required">
                            <option value="">请选择菜单类型</option>
                            <option value="0">一级菜单</option>
                            <option value="1">子菜单</option>
                            <option value="2">按钮权限</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-inline">
                        <input type="text" name="icon" placeholder="请输入图标名称" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">组件</label>
                    <div class="layui-input-inline">
                        <input type="text" name="component" placeholder="请输入组件名称" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">路径</label>
                    <div class="layui-input-inline">
                        <input type="text" name="url" placeholder="请输入路径" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-inline">
                        <input type="number" name="sortNo" placeholder="请输入排序号" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveAddBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 编辑表单 -->
        <div id="edit" style="display: none">
            <div class="layui-form layuimini-form" lay-filter="editForm">
                <div class="layui-form-item">
                    <label class="layui-form-label required">菜单ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="id" readonly class="layui-input layui-disabled">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">菜单名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" lay-verify="required" lay-reqtext="菜单名称不能为空"
                               placeholder="请输入菜单名称" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">菜单类型</label>
                    <div class="layui-input-inline">
                        <select name="menuType" lay-verify="required">
                            <option value="">请选择菜单类型</option>
                            <option value="0">一级菜单</option>
                            <option value="1">子菜单</option>
                            <option value="2">按钮权限</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-inline">
                        <input type="text" name="icon" placeholder="请输入图标名称" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">组件</label>
                    <div class="layui-input-inline">
                        <input type="text" name="component" placeholder="请输入组件名称" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">路径</label>
                    <div class="layui-input-inline">
                        <input type="text" name="url" placeholder="请输入路径" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-inline">
                        <input type="number" name="sortNo" placeholder="请输入排序号" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveEditBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        var index;

        // 初始化表格
        table.render({
            elem: '#currentTableId',
            url: '/sysPermission/queryPage',
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list
                };
            },
            toolbar: '#toolbarDemo',
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'name', width: 150, title: '菜单名称'},
                {field: 'menuType', width: 120, title: '菜单类型', templet: function(d) {
                        switch (d.menuType) {
                            case 0: return '一级菜单';
                            case 1: return '子菜单';
                            case 2: return '按钮权限';
                            default: return '';
                        }
                    }},
                {field: 'icon', width: 100, title: '图标'},
                {field: 'component', width: 150, title: '组件'},
                {field: 'url', width: 200, title: '路径'},
                {field: 'sortNo', width: 100, title: '排序'},
                {title: '操作', minWidth: 240, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 搜索监听
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: { curr: 1 },
                where: data.field
            }, 'data');
            return false;
        });

        // 工具栏监听
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {
                form.val("addForm", {});
                index = layer.open({
                    title: '添加菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['75%', '75%'],
                    content: $('#add'),
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        // 表格行操作监听
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                form.val("editForm", data);
                index = layer.open({
                    title: '编辑菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['75%', '75%'],
                    content: $('#edit'),
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                $.getJSON('/sysPermission/delete?id=' + data.id, function (res) {
                    if (res.code === 0) {
                        layer.msg("删除成功");
                        obj.del();
                    } else {
                        layer.msg("删除失败");
                    }
                });
            }
        });

        // 添加提交
        form.on('submit(saveAddBtn)', function (data) {
            $.post("/sysPermission/add", data.field, function (res) {
                if (res.code === 0) {
                    layer.msg("添加成功");
                    table.reload('currentTableId');
                } else {
                    layer.msg("添加失败", { icon: 2 });
                }
                layer.close(index);
            }, "json");
            return false;
        });

        // 编辑提交
        form.on('submit(saveEditBtn)', function (data) {
            $.post("/sysPermission/update", data.field, function (res) {
                if (res.code === 0) {
                    layer.msg("修改成功");
                    table.reload('currentTableId');
                } else {
                    layer.msg("修改失败", { icon: 2 });
                }
                layer.close(index);
            }, "json");
            return false;
        });

    });
</script>

</body>
</html>
